﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Dapper;

namespace UpdateYiChuanXmz
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();


        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.Visible = false;
            //lab5.Visible = false;
        }

        public static string QueryData(string sqlStr)
        {
            SqlConnection sqlConnection1 = new SqlConnection("server=192.168.20.251;database=lisdb20180423;uid=lisuser;pwd=lisuser");//创建数据库连接
            SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(sqlStr, sqlConnection1);//利用已创建好的sqlConnection1,创建数据适配器sqlDataAdapter1
            DataSet dataSet1 = new DataSet();  //创建数据集对象
            sqlDataAdapter1.Fill(dataSet1);    //执行查询,查询的结果存放在数据集里
            return dataSet1.Tables[0].Rows[0]["列名"].ToString(); //把查询结果的第一行指定列下的数据以string类型返回
        }

        private void button1_Click(object sender, EventArgs e)
        {

            panel1.Visible = true;
            //lab5.Visible = true;

            string constr = "data source=192.168.20.251;initial catalog=lisdb;user id=lisuser;pwd=lisuser";
            //string constr = "Data Source=192.168.20.251;Persist Security Info=True;User ID=lisuser;Initial Catalog=lisdb20180423";



            using (SqlConnection con = new SqlConnection(constr))
            {

                con.Open();
                //lab5.Text = "connect success";
                con.Close();


            }

            //这里开始连接数据库查询数据
            //if (textBox2.Text.Trim() == "")
            //{
            //    lab5.Text = "样本号不能为空";

            //}

            using (SqlConnection con = new SqlConnection(constr))
            {
                //3.sql语句
                string sql = "select xmcdz from lis_xmcdz where yqdh = '遗传' and ybbh  =" + textBox2.Text.Trim();

                //select* from "+ textBox1.Text.Trim(),conn
                //4.创建sqlcommand对象
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    //5.打开链接
                    con.Open();

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        //接下来就是要通过reader对象一条一条获取数据
                        //1.在获取数据前，先判断一下本次执行查询后，是否查到了数据
                        if (reader.HasRows)//如果有数据为true,否则为false
                        {
                            //2.如果有数据，那么接下来就要一条条获取数据
                            //每次获取数据之前，都要调用reader.Read()方法，向后移动一条数据，如果成功移动到了某条数据
                            //上，则返回true，否则返回false
                            while (reader.Read())
                            {

                                textBox1.Text = reader["xmcdz"].ToString();

                            }
                        }
                        else
                        {
                            //lab5.Text = "没有查到数据,请输入正确的样本号!";


                        }
                    }
                }
            }



        }



        private void textBox1_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //取出对应的值
            string constr = "data source=192.168.20.251;initial catalog=lisdb;user id=lisuser;pwd=lisuser";

            string ybbh = textBox2.Text.Trim();

            string xmcdz = textBox1.Text.Trim();


            //确认修改

            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
            IDbConnection conn = new SqlConnection(constr);

            lis_xmcdz cdzinfo = new lis_xmcdz();
            cdzinfo.ybbh = Convert.ToInt32(ybbh);
            cdzinfo.xmcdz = xmcdz;
            string upDateSql = "update dbo.lis_xmcdz set xmcdz = @xmcdz where ybbh = @ybbh ";

            var result = conn.Execute(upDateSql, cdzinfo);

            label6.Visible = true;


            //重新生成报告
            Lis_ReportTasks ReprotTask = new Lis_ReportTasks();
            ReprotTask.SampleNo =ybbh;
            ReprotTask.InstrumentId = "遗传";
            string Reprstring = "UPDATE dbo.Lis_ReportTasks SET ProcessFlag=0 WHERE SampleNo=@SampleNo and InstrumentId = @InstrumentId";

            var RePortres = conn.Execute(Reprstring, ReprotTask);
         
            if (RePortres > 0)
            {
                label6.Text = "更新报告成功，请注意核对报告单";
            }

        }

        private class lis_xmcdz
        {
            public int ybbh { get; internal set; }
            public string xmcdz { get; internal set; }
        }

        private class Lis_ReportTasks
        {
            public string SampleNo { get; internal set; }
            public string InstrumentId { get; internal set; }
        }
    }


    

}
